package com.yuke.cloud.service.wmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.uac.dto.SupplierApiDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.dao.WmsFlowMapper;
import com.yuke.cloud.service.wmc.dao.WmsPurchaseOrderMapper;
import com.yuke.cloud.service.wmc.dao.WmsPurchaseReturnOrderMapper;
import com.yuke.cloud.service.wmc.dto.WmsFlowDto;
import com.yuke.cloud.service.wmc.entity.WmsFlow;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseOrder;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseReturnOrder;
import com.yuke.cloud.service.wmc.service.WmsFlowService;
import com.yuke.cloud.service.wmc.service.WmsPurchaseOrderService;
import com.yuke.cloud.service.wmc.service.WmsPurchaseReturnOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/11/17.
 */
@Service
@Transactional
public class WmsFlowServiceImpl extends BaseService<WmsFlow> implements WmsFlowService {
    @Resource
    private WmsFlowMapper wmsFlowMapper;
    @Resource
    private WmsPurchaseOrderMapper wmsPurchaseOrderMapper;
    @Resource
    private WmsPurchaseOrderService wmsPurchaseOrderService;
    @Resource
    private WmsPurchaseReturnOrderMapper wmsPurchaseReturnOrderMapper;
    @Resource
    private WmsPurchaseReturnOrderService wmsPurchaseReturnOrderService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;

    //未对流水表做连表 暂不用
    @Override
    public List<WmsFlow> selectCondition(WmsFlowDto wmsFlowDto) {
        //根据供应商判断
        if (wmsFlowDto.getSupplierId()!=null &&wmsFlowDto.getSupplierId().equals(0L) ) {
            //根据供应商查询进货单
            if (wmsFlowDto.getFlowType().equals((byte)1)) {
                Example example = new Example(WmsPurchaseOrder.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("supplierId", wmsFlowDto.getSupplierId())
                        .andEqualTo("isDeleted", wmsFlowDto.getFlowIsDeleted());
                List<WmsPurchaseOrder> wmsPurchaseOrders = wmsPurchaseOrderMapper.selectByExample(example);
                List<Long> collect = wmsPurchaseOrders.stream().map(WmsPurchaseOrder::getPurchaseId).collect(Collectors.toList());
                wmsFlowDto.setPurchaseIdList(collect);

            }else if (wmsFlowDto.getFlowType().equals((byte)2)){
                //根据供应商查询进货退货单
                Example example = new Example(WmsPurchaseReturnOrder.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("supplierId", wmsFlowDto.getSupplierId())
                        .andEqualTo("isDeleted", wmsFlowDto.getFlowIsDeleted());
                List<WmsPurchaseReturnOrder> wmsPurchaseOrders = wmsPurchaseReturnOrderMapper.selectByExample(example);
                List<Long> collect = wmsPurchaseOrders.stream().map(WmsPurchaseReturnOrder::getPurchaseReturnId).collect(Collectors.toList());
                wmsFlowDto.setPurchaseIdList(collect);
            }else{

                Example example = new Example(WmsPurchaseOrder.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("supplierId", wmsFlowDto.getSupplierId())
                        .andEqualTo("isDeleted", wmsFlowDto.getFlowIsDeleted());
                List<WmsPurchaseOrder> wmsPurchaseOrders = wmsPurchaseOrderMapper.selectByExample(example);
                List<Long> purchaseOrderList = wmsPurchaseOrders.stream().map(WmsPurchaseOrder::getPurchaseId).collect(Collectors.toList());

                Example example2 = new Example(WmsPurchaseReturnOrder.class);
                Example.Criteria criteria2 = example2.createCriteria();
                criteria2.andEqualTo("supplierId", wmsFlowDto.getSupplierId())
                        .andEqualTo("isDeleted", wmsFlowDto.getFlowIsDeleted());
                List<WmsPurchaseReturnOrder> wmsPurchaseReturnOrder = wmsPurchaseReturnOrderMapper.selectByExample(example);
                List<Long> wmsPurchaseReturnList = wmsPurchaseReturnOrder.stream().map(WmsPurchaseReturnOrder::getPurchaseReturnId).collect(Collectors.toList());

                wmsFlowDto.setPurchaseReturnIdList(wmsPurchaseReturnList);
                wmsFlowDto.setPurchaseIdList(purchaseOrderList);

            }
            List<WmsFlow> list = wmsFlowMapper.selectCondition(wmsFlowDto);
            return list;

        }else {
            //不根据供应商判断
            List<WmsFlow> list = wmsFlowMapper.selectCondition(wmsFlowDto);

            return list;
        }
    }



    @Override
    public Integer hongchong(WmsFlowDto wmsFlowDto) {
        //更新 进货单实付
        //查
        Integer update = 0;
        if (wmsFlowDto.getFlowType().equals((byte)1)) {
            WmsPurchaseOrder wmsPurchaseOrder = wmsPurchaseOrderMapper.selectByPrimaryKey(wmsFlowDto.getPurchaseId());
            Integer payment = wmsPurchaseOrder.getPayment();
            payment -= wmsFlowDto.getFlowMoney();
            wmsPurchaseOrder.setPayment(payment);
            update = wmsPurchaseOrderService.update(wmsPurchaseOrder);
        }else if (wmsFlowDto.getFlowType().equals((byte)2)) {
            //跟新进货退货单实付
            WmsPurchaseReturnOrder wmsPurchaseReturnOrder = wmsPurchaseReturnOrderMapper.selectByPrimaryKey(wmsFlowDto.getPurchaseReturnId());
            Integer receipts = wmsPurchaseReturnOrder.getReceipts();
            receipts -= wmsFlowDto.getFlowMoney();
            wmsPurchaseReturnOrder.setReceipts(receipts);
            update = wmsPurchaseReturnOrderService.update(wmsPurchaseReturnOrder);
        }
        return update;
    }

    @Override
    public List<WmsFlowDto> selectFlowIn(WmsFlowDto wmsFlowDto) {

        List<WmsFlowDto> list = wmsFlowMapper.selectFlowIn(wmsFlowDto);

        if (list==null||list.size()==0){
            return null;
        }
        List<Long> collect = list.stream().map(WmsFlowDto::getSupplierId).collect(Collectors.toList());

        List<SupplierApiDto> supplierByIds = uacUserFeignApi.findSupplierByIds(collect);

        if (supplierByIds!=null&&supplierByIds.size()>0) {
            for (WmsFlowDto flowDto : list) {
                for (SupplierApiDto supplierById : supplierByIds) {
                    if (flowDto.getSupplierId().equals(supplierById.getSupplierId())) {
                        flowDto.setSupplierName(supplierById.getSupplierName());
                    }
                }
            }
        }

        return list;
    }


}
